package com.test.travelinformation.dao;


import android.util.Log;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.test.travelinformation.entity.Collect;
import com.test.travelinformation.entity.Travel;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobQueryResult;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.SQLQueryListener;

public class TravelDao {
    //我的收藏
    private List<Travel> collectData;
    //我的发布
    private List<Travel> issueData;
    //所有信息
    private List<Travel> allData;

    /**
     * 同步请求，返回数据
     * 获取我收藏的信息
     * @param userId 用户ID （本地有存储  userId）
     * @return 收藏的信息
     */
    public List<Travel> getCollectDataSync(String userId){
        try {
            Thread thread = new Thread(() -> {
                BmobQuery<Collect> collectQuery = new BmobQuery<>();
                collectQuery.addWhereEqualTo("userId",userId);
                List<Collect> collectList = collectQuery.findObjectsSync(Collect.class);
                for (Collect c : collectList) {
                    BmobQuery<Travel> travelQuery = new BmobQuery<>();
                    travelQuery.addWhereEqualTo("objectId",c.getTravelId());
                    List<Travel> travelList = travelQuery.findObjectsSync(Travel.class);
                    collectData.addAll(travelList);
                }
            });
            thread.start();
            thread.join();
        }catch (Exception e){
            collectData = new ArrayList<>();
        }
        return collectData;
    }

    /**
     * 异步请求，直接设置Adapter数据
     * 获取我收藏的信息
     * @param userId 用户ID （本地有存储  userId）
     */
    public void getCollectData(BaseQuickAdapter adapter, String userId){
        String bql = "select * from Collect where userId='"+userId+"' order by -updatedAt";
        BmobQuery<Collect> collectQuery = new BmobQuery<>();
        collectQuery.doSQLQuery(bql, new SQLQueryListener<Collect>() {
            @Override
            public void done(BmobQueryResult<Collect> bmobQueryResult, BmobException e) {
                if (e == null){
                    for (Collect c : bmobQueryResult.getResults()) {
                        BmobQuery<Travel> travelQuery = new BmobQuery<>();
                        travelQuery.getObject(c.getTravelId(), new QueryListener<Travel>() {
                            @Override
                            public void done(Travel travel, BmobException e) {
                                if (e == null){
                                    adapter.addData(travel);
                                }else {
                                    Log.e("错误2",e.toString());
                                }
                            }
                        });
                    }
                }else {
                    Log.e("错误1",e.toString());
                }
            }
        });
//        collectQuery.findObjects(new FindListener<Collect>() {
//            @Override
//            public void done(List<Collect> list, BmobException e) {
//                if (e == null){
//                    for (Collect c : list) {
//                        BmobQuery<Travel> travelQuery = new BmobQuery<>();
//                        travelQuery.getObject(c.getTravelId(), new QueryListener<Travel>() {
//                            @Override
//                            public void done(Travel travel, BmobException e) {
//                                if (e == null){
//                                    adapter.addData(travel);
//                                }else {
//                                    Log.e("错误",e.toString());
//                                }
//                            }
//                        });
//                    }
//                }else {
//                    Log.e("错误",e.toString());
//                }
//            }
//        });
    }

    /**
     * 异步请求，直接设置Adapter数据
     * 获取我发布的信息
     * @param userId 用户ID （本地有存储  userId）
     * @return 发布的信息
     */
    public void getIssueData(BaseQuickAdapter adapter,String userId){
        BmobQuery<Travel> query = new BmobQuery<>();
        query.addWhereEqualTo("userId",userId);
        query.findObjects(new FindListener<Travel>() {
            @Override
            public void done(List<Travel> list, BmobException e) {
                if (e == null){
                    adapter.addData(list);
                }
            }
        });
    }

    /**
     * 同步请求，返回数据
     * 获取我发布的信息
     * @param userId 用户ID （本地有存储  userId）
     * @return 发布的信息
     */
    public List<Travel> getIssueDataSync(String userId){
        try {
            Thread thread = new Thread(() -> {
                BmobQuery<Travel> query = new BmobQuery<>();
                query.addWhereEqualTo("userId",userId);
                List<Travel> list = query.findObjectsSync(Travel.class);
                issueData = list;
            });
            thread.start();
            thread.join();
        }catch (Exception e){
            issueData = new ArrayList<>();
        }
        return issueData;
    }

    /**
     * 异步请求，直接设置Adapter数据
     * 获取所有信息
     * @return 所有发布的信息
     */
    public void getAllData(BaseQuickAdapter adapter){
        String bql = "select * from Travel order by -updatedAt";
        BmobQuery<Travel> query = new BmobQuery<>();
        query.doSQLQuery(bql, new SQLQueryListener<Travel>() {
            @Override
            public void done(BmobQueryResult<Travel> bmobQueryResult, BmobException e) {
                if (e == null){
                    List<Travel> list = bmobQueryResult.getResults();
                    adapter.addData(list);
                }
            }
        });
    }

    /**
     * 同步请求，返回数据
     * 获取所有信息
     * @return 所有发布的信息
     */
    public List<Travel> getAllDataSync(){
        try {
            Thread thread = new Thread(() -> {
                BmobQuery<Travel> query = new BmobQuery<>();
                allData = query.findObjectsSync(Travel.class);
            });
            thread.start();
            thread.join();
        }catch (Exception e){
            allData = new ArrayList<>();
        }
        return allData;
    }
}
